package com.liuqi.knet.rpc.cluster.loadbalance;

import com.liuqi.knet.remoting.RemotingException;
import com.liuqi.knet.rpc.cluster.AbstractLoadBalance;
import com.liuqi.knet.rpc.cluster.ClusterConfig;
import com.liuqi.knet.rpc.cluster.LoadBalanceNode;
import java.util.List;

/**
 * 随机
 *
 * @author liuqi
 * @date 2024/6/20 10:44
 **/
public class RandomLoadBalance extends AbstractLoadBalance {

    public RandomLoadBalance(ClusterConfig clusterConfig) {
        super(clusterConfig);
    }

    @Override
    protected LoadBalanceNode.Node doSelect() throws RemotingException {
        List<LoadBalanceNode.Node> nodes = getLoadBalanceNode().getNodes();
        int index = (int) (Math.random() * nodes.size());
        return nodes.get(index);
    }

}
